package com.chatroom.controller;

import com.chatroom.dto.ChatRoomDTO;
import com.chatroom.dto.UserDTO;
import com.chatroom.model.ChatRoom;
import com.chatroom.model.User;
import com.chatroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/users")
public class UserChatRoomController {

    private final UserService userService;

    @Autowired
    public UserChatRoomController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{userId}/chatrooms")
    public ResponseEntity<?> getUserChatRooms(@PathVariable Long userId) {
        Optional<User> userOpt = userService.findById(userId);
        
        if (userOpt.isPresent()) {
            List<ChatRoomDTO> chatRooms = userOpt.get().getChatRooms().stream()
                    .map(ChatRoomDTO::new)
                    .collect(Collectors.toList());
            return ResponseEntity.ok(chatRooms);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/{userId}")
    public ResponseEntity<?> getUser(@PathVariable Long userId) {
        Optional<User> userOpt = userService.findById(userId);
        
        if (userOpt.isPresent()) {
            return ResponseEntity.ok(new UserDTO(userOpt.get()));
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 